package cn.mybatis.service;

import cn.school.mybatis.Util.MyBatisUtil;
import cn.school.mybatis.dao.AdminDao;
import cn.school.mybatis.pojo.Admin;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//业务逻辑bean
//服务层,调用dao方法
public class AdminService {

    public final static AdminService ADMIN_SERVICE = new AdminService();

//增加
public int add(Admin admin) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        AdminDao admindao = session.getMapper(AdminDao.class);
        return admindao.add(admin);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//删除
public int del(int id) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        AdminDao dao = session.getMapper(AdminDao.class);
        return dao.del(id);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//修改
public int update(Admin admin) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        AdminDao dao = session.getMapper(AdminDao.class);
        return dao.update(admin);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//id查单个
public Admin get(int id) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        AdminDao dao = session.getMapper(AdminDao.class);
        return dao.get(id);
    } finally {
        MyBatisUtil.close(session);
    }
}

    //账号密码查单个
    public Admin getUser(String username,String password) {
        SqlSession session = MyBatisUtil.getSqlSession();
        Map map=new HashMap();
        map.put("username",username);
        map.put("password",password);
        try {
            AdminDao dao = session.getMapper(AdminDao.class);
            return dao.searchUser(map);
        } finally {
            MyBatisUtil.close(session);
        }
    }
//查全部
public List<Admin> searchAll() {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        AdminDao dao = session.getMapper(AdminDao.class);
        return dao.searchAll();
    } finally {
        MyBatisUtil.close(session);
    }
}




}
